package demo.exercise;

import java.io.*;
import java.util.Properties;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-07-29
 */
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        String fileParent = "D:\\CScode\\code\\java_code\\2024\\t0729\\src";
        //创建目录
        File file = new File(fileParent, "mytemp");
        if (file.exists()) {
            System.out.println("该目录已经存在");
        } else {
            file.mkdir();
        }
        //创建文件
        File file2 = new File(file, "hello.txt");
        if (file2.exists()) {
            System.out.println("该文件已经存在");
        } else {
            try {
                file2.createNewFile();
            } catch (IOException e) {
                System.out.println("创建文件失败");
            }
        }
        //读取文件
        BufferedReader bufIn = null;
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file2), "gbk");
        String line;
        int count = 0;
        try {
            bufIn = new BufferedReader(isr);
            while ((line = bufIn.readLine()) != null) {
                System.out.println((++count) + line);
            }
        } catch (IOException e){
            System.out.println("创建失败");
            e.printStackTrace();
        }
        //读取配置文件
        Properties dogIn = new Properties();
        try {
            dogIn.load(new FileReader(new File(fileParent, "dog.properties")));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        Dog dog = new Dog(dogIn.getProperty("name"), Integer.parseInt(dogIn.getProperty("age")), dogIn.getProperty("color"));
        System.out.println(dog);

        //数据序列化
        String fileDate = fileParent + "\\dog.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileDate));
        oos.writeObject(dog);
        oos.close();

        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileDate));
        Object data = ois.readObject();
        System.out.println((Dog)data);
    }
}
